package com.example.my_self_view

import org.junit.Test


/**
 * Example local unit test, which will execute on the development machine (host).
 *
 * See [testing documentation](http://d.android.com/tools/testing).
 */
class ExampleUnitTest {
    @Test
    fun addition_isCorrect() {
        val rectA = Rectangle().apply {
            pointList.add(Point(1,0))
            pointList.add(Point(3, 0))
            pointList.add(Point(1, 2))
            pointList.add(Point(3, 2))
        }
        val rectB = Rectangle().apply {
            pointList.add(Point(1, 1))
            pointList.add(Point(3, 1))
            pointList.add(Point(1, 3))
            pointList.add(Point(3, 3))
        }
        println(isCoverTowRectangle(rectA, rectB))
    }

    private fun isCoverTowRectangle(rectA: Rectangle, rectB: Rectangle): Boolean {
        if (rectA.pointList[0] == rectB.pointList[0] && rectA.pointList[1] == rectB.pointList[1]
            && rectA.pointList[2] == rectB.pointList[2] && rectA.pointList[3] == rectB.pointList[3]) return true
        // 两个矩形必有一个顶点在另一个矩形内
        var scoreRectA = 0
        for (pointA in rectA.pointList) { // 主B
            if (pointA.x > rectB.pointList[0].x && pointA.x < rectB.pointList[1].x
                && pointA.y > rectB.pointList[0].y && pointA.y < rectB.pointList[2].y) {
                return true
            }
            if (pointA.y >= rectB.pointList[0].y && pointA.y <= rectB.pointList[2].y) {
                if (pointA.x == rectB.pointList[0].x || pointA.x == rectB.pointList[1].x) {
                    scoreRectA++
                }
            }
            if (pointA.x >= rectB.pointList[0].x && pointA.x <= rectB.pointList[1].x) {
                if (pointA.y == rectB.pointList[0].y || pointA.y == rectB.pointList[2].y) {
                    scoreRectA++
                }
            }
        }
        if (scoreRectA >= 2) {
            // 判断是否踩点重叠
            // 1.左
            if (rectA.pointList[1] == rectB.pointList[0] && rectA.pointList[3] == rectB.pointList[2]) {
                return false
            }
            // 2.上
            if (rectA.pointList[2] == rectB.pointList[0] && rectA.pointList[3] == rectB.pointList[1]) {
                return false
            }
            // 3.右
            if (rectA.pointList[0] == rectB.pointList[1] && rectA.pointList[2] == rectB.pointList[3]) {
                return false
            }
            // 4.下
            if (rectA.pointList[0] == rectB.pointList[2] && rectA.pointList[1] == rectB.pointList[3]) {
                return false
            }
            return true
        }

        var scoreRectB = 0
        for (pointB in rectB.pointList) { // 主 A
            if (pointB.x > rectA.pointList[0].x && pointB.x < rectA.pointList[1].x
                && pointB.y > rectA.pointList[0].y && pointB.y < rectA.pointList[2].y) {
                return true
            }
            if (pointB.y >= rectA.pointList[0].y && pointB.y <= rectA.pointList[2].y) {
                if (pointB.x == rectA.pointList[0].x || pointB.x == rectA.pointList[1].x) {
                    scoreRectB++
                }
            }
            if (pointB.x >= rectA.pointList[0].x && pointB.x <= rectA.pointList[1].x) {
                if (pointB.y == rectA.pointList[0].y || pointB.y == rectA.pointList[2].y) {
                    scoreRectB++
                }
            }
        }

        if (scoreRectB >= 2) {
            // 判断是否踩点重叠
            // 1.左
            if (rectA.pointList[1] == rectB.pointList[0] && rectA.pointList[3] == rectB.pointList[2]) {
                return false
            }
            // 2.上
            if (rectA.pointList[2] == rectB.pointList[0] && rectA.pointList[3] == rectB.pointList[1]) {
                return false
            }
            // 3.右
            if (rectA.pointList[0] == rectB.pointList[1] && rectA.pointList[2] == rectB.pointList[3]) {
                return false
            }
            // 4.下
            if (rectA.pointList[0] == rectB.pointList[2] && rectA.pointList[1] == rectB.pointList[3]) {
                return false
            }
            return true
        }
        return false
    }

    private inner class Rectangle {
        var pointList = arrayListOf<Point>()
    }

    private inner class Point() {
        var x = 0
        var y = 0

        constructor(x: Int, y: Int): this() {
            this.x = x
            this.y = y
        }

        override fun equals(other: Any?): Boolean {
            if (this === other) return true
            if (javaClass != other?.javaClass) return false

            other as Point

            if (x != other.x) return false
            if (y != other.y) return false

            return true
        }

        override fun hashCode(): Int {
            var result = x
            result = 31 * result + y
            return result
        }

        override fun toString(): String {
            return "Point(x=$x, y=$y)"
        }


    }
}